#/************************************************************************
#*   IRC - Internet Relay Chat, Makefile
#*   Copyright (C) 1990, Jarkko Oikarinen
#*
#*   This program is free software; you can redistribute it and/or modify
#*   it under the terms of the GNU General Public License as published by
#*   the Free Software Foundation; either version 1, or (at your option)
#*   any later version.
#*
#*   This program is distributed in the hope that it will be useful,
#*   but WITHOUT ANY WARRANTY; without even the implied warranty of
#*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#*   GNU General Public License for more details.
#*
#*   You should have received a copy of the GNU General Public License
#*   along with this program; if not, write to the Free Software
#*   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#*  
#*   $Id$
#*/

CC=@CC@
INCLUDEDIR=@UNRLINCDIR@
NETWORKSDIR=
FROMDOS=/home/cmunk/bin/4dos

# [CHANGEME]
# Default flags:
# Change XCFLAGS if you don't like what Config puts there.  Same with
# IRCDLIBS.
#
# If you are configuring by hand, try "-O -g" for XCFLAGS, and leave
# IRCDLIBS blank.  If that fails, try recomendations below.
#

#XCFLAGS=-O -g -export-dynamic
IRCDLIBS=@IRCDLIBS@ @PCRE2_LIBS@ @ARGON2_LIBS@ @CARES_LIBS@ @PTHREAD_LIBS@ -lpq -lm -lcurl
CRYPTOLIB=@CRYPTOLIB@
OPENSSLINCLUDES=

XCFLAGS=@PTHREAD_CFLAGS@ @PCRE2_CFLAGS@  @ARGON2_CFLAGS@ @CARES_CFLAGS@ @CFLAGS@ @HARDEN_CFLAGS@ @CPPFLAGS@ -I/usr/include/postgresql 
#
# use the following on MIPS:
#CFLAGS= -systype bsd43 -DSYSTYPE_BSD43 -I$(INCLUDEDIR)
# For Irix 4.x (SGI), use the following:
#CFLAGS= -g -cckr -I$(INCLUDEDIR)
#
# on NEXT use:
#CFLAGS=-bsd -I$(INCLUDEDIR)
#on NeXT other than 2.0:
#IRCDLIBS=-lsys_s
#
# AIX 370 flags
#CFLAGS=-D_BSD -Hxa -I$(INCLUDEDIR)
#IRCDLIBS=-lbsd
#
# Dynix/ptx V2.0.x
#CFLAGS= -I$(INCLUDEDIR) -O -Xo
#IRCDLIBS= -lsocket -linet -lnsl -lseq
# 
# Dynix/ptx V1.x.x
#IRCDLIBS= -lsocket -linet -lnsl -lseq
#
#use the following on SUN OS without nameserver libraries inside libc
#IRCDLIBS=-lresolv
#
# Solaris 2
#IRCDLIBS=-lsocket -lnsl -lresolv -L/usr/ucblib -R/usr/ucblib -lgen
#
# ESIX
#CFLAGS=-O -I$(INCLUDEDIR) -I/usr/ucbinclude
#IRCDLIBS=-L/usr/ucblib -L/usr/lib -lsocket -lucb -lns -lnsl
#
# LDFLAGS - flags to send the loader (ld). SunOS users may want to add
# -Bstatic here.
#
#LDFLAGS=-Bstatic
#
#Dell SVR4
#CC=gcc
#CFLAGS= -I$(INCLUDEDIR) -O2
#IRCDLIBS=-lsocket -lnsl -lucb

# [CHANGEME]
# IRCDMODE is the mode you want the binary to be.
# The 4 at the front is important (allows for setuidness)
#
# WARNING: if you are making ircd SUID or SGID, check config.h to make sure
#          you are not defining CMDLINE_CONFIG 
IRCDMODE = 711

URL=@URL@

# Where is your openssl binary
OPENSSLPATH=@OPENSSLPATH@

CFLAGS=-I$(INCLUDEDIR) $(XCFLAGS)
XLDFLAGS=@LDFLAGS_PRIVATELIBS@ @HARDEN_LDFLAGS@ @LDFLAGS@
LDFLAGS=$(XLDFLAGS)

SHELL=/bin/sh
SUBDIRS=src
BINDIR=@BINDIR@
INSTALL=@INSTALL@
RM=@RM@
CP=@CP@
TOUCH=@TOUCH@
RES=
all:	build

MAKEARGS =	'CFLAGS=${CFLAGS}' 'CC=${CC}' 'IRCDLIBS=${IRCDLIBS}' \
		'LDFLAGS=${LDFLAGS}' 'IRCDMODE=${IRCDMODE}' \
		'RES=${RES}' 'BINDIR=${BINDIR}' 'INSTALL=${INSTALL}' \
		'INCLUDEDIR=${INCLUDEDIR}' \
		'RM=${RM}' 'CP=${CP}' 'TOUCH=${TOUCH}' \
		'SHELL=${SHELL}' \
		'CRYPTOLIB=${CRYPTOLIB}' \
		'CRYPTOINCLUDES=${CRYPTOINCLUDES}' \
		'URL=${URL}'

custommodule:
	@if test -z "${MODULEFILE}"; then echo "Please set MODULEFILE when calling \`\`make custommodule''. For example, \`\`make custommodule MODULEFILE=callerid''." >&2; exit 1; fi
	+cd src; ${MAKE} ${MAKEARGS} MODULEFILE=${MODULEFILE} 'EXLIBS=${EXLIBS}' custommodule

server: Makefile
build: Makefile
	-@if [ ! -f include/setup.h ] ; then \
		echo "Hmm...doesn't look like you've run Config..."; \
		echo "Doing so now."; \
		sh Config; \
	fi
	@+for i in $(SUBDIRS); do \
		echo "Building $$i";\
		( cd $$i; ${MAKE} ${MAKEARGS} build; ) \
	done
	@echo ''
	@echo '* UnrealIRCd compiled successfully'
	@echo '* YOU ARE NOT DONE YET! Run "make install" to install UnrealIRCd !'
	@echo ''

clean:
	$(RM) -f *~ \#* core *.orig include/*.orig
	@+for i in $(SUBDIRS); do \
		echo "Cleaning $$i";\
		( cd $$i; ${MAKE} ${MAKEARGS} clean; ) \
	done
	-@if [ -f include/setup.h ] ; then \
	  echo "To really restart installation, remove include/setup.h" ; \
	fi

cleandir: clean
	rm -rf include/setup.h Makefile Settings

distclean: cleandir
	rm -rf extras/*.bak extras/*.tar extras/c-ares
	rm -rf extras/c-ares-* extras/tre-*
	rm -rf config.log config.settings *.pem ircd.* unrealircd
	rm -rf Makefile config.status
	
depend:
	@+for i in $(SUBDIRS); do \
		echo "Making dependencies in $$i";\
		( cd $$i; ${MAKE} ${MAKEARGS} depend; ) \
	done

install: all
	$(INSTALL) -m 0700 -d $(DESTDIR)@BINDIR@
	$(INSTALL) -m 0700 src/ircd $(DESTDIR)@BINDIR@/unrealircd
	$(INSTALL) -m 0700 -d $(DESTDIR)@DOCDIR@
	$(INSTALL) -m 0600 doc/Authors doc/coding-guidelines doc/tao.of.irc $(DESTDIR)@DOCDIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@CONFDIR@; 

#	$(INSTALL) -m 0600 doc/conf/*.default.conf $(DESTDIR)@CONFDIR@; 
#	$(INSTALL) -m 0600 doc/conf/*.optional.conf $(DESTDIR)@CONFDIR@; 

	-@if [ ! -f "$(DESTDIR)@CONFDIR@/modules.sources.list" ] ; then \
		$(INSTALL) -m 0600 doc/conf/modules.sources.list $(DESTDIR)@CONFDIR@ ; \
	fi
	-@if [ ! -f "$(DESTDIR)@CONFDIR@/spamfilter.conf" ] ; then \
		$(INSTALL) -m 0600 doc/conf/spamfilter.conf $(DESTDIR)@CONFDIR@ ; \
	fi
	-@extras/patches/patch_spamfilter_conf "$(DESTDIR)@CONFDIR@"
	-@if [ ! -f "$(DESTDIR)@CONFDIR@/badwords.conf" ] ; then \
		$(INSTALL) -m 0600 doc/conf/badwords.conf $(DESTDIR)@CONFDIR@ ; \
	fi
	-@if [ ! -f "$(DESTDIR)@CONFDIR@/dccallow.conf" ] ; then \
		$(INSTALL) -m 0600 doc/conf/dccallow.conf $(DESTDIR)@CONFDIR@ ; \
	fi
	$(INSTALL) -m 0700 -d $(DESTDIR)@CONFDIR@/aliases
	$(INSTALL) -m 0600 doc/conf/aliases/*.conf $(DESTDIR)@CONFDIR@/aliases
	$(INSTALL) -m 0700 -d $(DESTDIR)@CONFDIR@/help
	$(INSTALL) -m 0600 doc/conf/help/*.conf $(DESTDIR)@CONFDIR@/help
	$(INSTALL) -m 0700 -d $(DESTDIR)@CONFDIR@/examples
	$(INSTALL) -m 0600 doc/conf/examples/*.conf $(DESTDIR)@CONFDIR@/examples
	$(INSTALL) -m 0700 unrealircd $(DESTDIR)@SCRIPTDIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@
	@rm -f $(DESTDIR)@MODULESDIR@/*.so 1>/dev/null 2>&1
	$(INSTALL) -m 0700 src/modules/*.so $(DESTDIR)@MODULESDIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@/usermodes
	@rm -f $(DESTDIR)@MODULESDIR@/usermodes/*.so 1>/dev/null 2>&1
	$(INSTALL) -m 0700 src/modules/usermodes/*.so $(DESTDIR)@MODULESDIR@/usermodes
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@/chanmodes
	@rm -f $(DESTDIR)@MODULESDIR@/chanmodes/*.so 1>/dev/null 2>&1
	$(INSTALL) -m 0700 src/modules/chanmodes/*.so $(DESTDIR)@MODULESDIR@/chanmodes
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@/snomasks
	@rm -f $(DESTDIR)@MODULESDIR@/snomasks/*.so 1>/dev/null 2>&1
	$(INSTALL) -m 0700 src/modules/snomasks/*.so $(DESTDIR)@MODULESDIR@/snomasks
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@/extbans
	@rm -f $(DESTDIR)@MODULESDIR@/extbans/*.so 1>/dev/null 2>&1
	$(INSTALL) -m 0700 src/modules/extbans/*.so $(DESTDIR)@MODULESDIR@/extbans
	@#If the conf/ssl directory exists then rename it here to conf/tls
	@#and add a symlink for backwards compatibility (so that f.e. certbot
	@#doesn't randomly fail after an upgrade to U5).
	-@if [ -d "$(DESTDIR)@CONFDIR@/ssl" ] ; then \
		mv "$(DESTDIR)@CONFDIR@/ssl" "$(DESTDIR)@CONFDIR@/tls" ; \
		ln -s "$(DESTDIR)@CONFDIR@/tls" "$(DESTDIR)@CONFDIR@/ssl" ; \
	fi
	$(INSTALL) -m 0700 -d $(DESTDIR)@CONFDIR@/tls
	$(INSTALL) -m 0600 doc/conf/tls/curl-ca-bundle.crt $(DESTDIR)@CONFDIR@/tls
	@# delete modules/cap directory, to avoid confusing with U4 to U5 upgrades:
	rm -rf $(DESTDIR)@MODULESDIR@/cap
	$(INSTALL) -m 0700 -d $(DESTDIR)@MODULESDIR@/third
	@rm -f $(DESTDIR)@MODULESDIR@/third/*.so 1>/dev/null 2>&1
	@#This step can fail with zero files, so we ignore exit status:
	-$(INSTALL) -m 0700 src/modules/third/*.so $(DESTDIR)@MODULESDIR@/third
	$(INSTALL) -m 0700 -d $(DESTDIR)@TMPDIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@CACHEDIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@PERMDATADIR@
	$(INSTALL) -m 0700 -d $(DESTDIR)@LOGDIR@
	-@if [ ! -f "$(DESTDIR)@CONFDIR@/tls/server.cert.pem" ] ; then \
		$(INSTALL) -m 0600 server.req.pem $(DESTDIR)@CONFDIR@/tls ; \
		$(INSTALL) -m 0600 server.key.pem $(DESTDIR)@CONFDIR@/tls ; \
		$(INSTALL) -m 0600 server.cert.pem $(DESTDIR)@CONFDIR@/tls ; \
	fi
	@rm -f $(DESTDIR)@SCRIPTDIR@/source
	ln -s @BUILDDIR@ $(DESTDIR)@SCRIPTDIR@/source
	@echo ''
	@echo '* UnrealIRCd is now installed.'
	
	-@if [ "@SCRIPTDIR@/bin" = "@BINDIR@" ] ; then \
		echo '* Leave this directory and run "cd @SCRIPTDIR@" now' ; \
	fi
	@echo '* Directory layout:'
	-@if [ "@SCRIPTDIR@/bin" = "@BINDIR@" ] ; then \
		echo ' * Base directory: @SCRIPTDIR@' ; \
	fi
	@echo '  * Configuration files: @CONFDIR@'
	@echo '  * Log files: @LOGDIR@'
	@echo '  * Modules: @MODULESDIR@'
	@echo '* To start/stop UnrealIRCd run: @SCRIPTDIR@/unrealircd"'
	@echo ''
	@echo '* Consult the documentation online at:'
	@echo '  * https://www.unrealircd.org/docs/'
	@echo '  * https://www.unrealircd.org/docs/FAQ'
	@echo '* You may also wish to install a cron job to ensure UnrealIRCd is always running:'
	@echo '  * https://www.unrealircd.org/docs/Cron_job'
	@echo ''
	-@if [ "@SCRIPTDIR@/bin" = "@BINDIR@" ] ; then \
		echo 'Again, be sure to change to the @SCRIPTDIR@ directory!' ; \
	fi

pem:	extras/tls.cnf
	@echo "Generating server key..."
	$(OPENSSLPATH) ecparam -out server.key.pem -name secp384r1 -genkey
	@echo "Generating certificate request..."
	$(OPENSSLPATH) req -new \
              -config extras/tls.cnf -sha256 -out server.req.pem \
              -key server.key.pem -nodes
	@echo "Generating self-signed certificate..."
	$(OPENSSLPATH) req -x509 -days 3650 -sha256 -in server.req.pem \
               -key server.key.pem -out server.cert.pem
	@echo "Setting permissions on server.*.pem files..."
	chmod o-rwx server.req.pem server.key.pem server.cert.pem
	chmod g-rwx server.req.pem server.key.pem server.cert.pem

Makefile: config.status Makefile.in
	./config.status
